package com.ruoyi.server.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.server.domain.dto.ServerRegionAddDTO;
import com.ruoyi.server.domain.dto.ServerRegionPageDTO;
import com.ruoyi.server.domain.dto.ServerRegionUpdateDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.server.mapper.ServerRegionMapper;
import com.ruoyi.server.domain.ServerRegion;
import com.ruoyi.server.service.IServerRegionService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 区服Service业务层处理
 *
 * @author ghostmen
 * @date 2024-10-31
 */
@Service
public class ServerRegionServiceImpl extends ServiceImpl<ServerRegionMapper, ServerRegion> implements IServerRegionService {
    @Autowired
    private ServerRegionMapper serverRegionMapper;

    /**
     * 查询区服
     *
     * @param regionId 区服主键
     * @return 区服
     */
    @Override
    public ServerRegion selectServerRegionByRegionId(Long regionId) {
        return serverRegionMapper.selectServerRegionByRegionId(regionId);
    }

    /**
     * 查询区服列表
     *
     * @param page 区服
     * @return 区服
     */
    @Override
    public List<ServerRegion> selectServerRegionList(ServerRegionPageDTO page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return serverRegionMapper.selectServerRegionList(page);
    }

    /**
     * 新增区服
     *
     * @param addDTO 区服
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertServerRegion(ServerRegionAddDTO addDTO) {
        //判断是否存在
        Long count = serverRegionMapper.selectCount(new QueryWrapper<ServerRegion>().lambda()
                .eq(ServerRegion::getRegionName, addDTO.getRegionName())
        );
        if (count > 0) {
            throw new ServiceException("该区服《" + addDTO.getRegionName() + "》已存在，请勿重复添加");
        }
        ServerRegion serverRegion = BeanUtil.copyProperties(addDTO, ServerRegion.class);
        serverRegion.setRegionId(IdWorker.getId());
        serverRegion.setCreateBy(addDTO.getLoginUser().getUsername());
        serverRegion.setCreateTime(DateUtils.getNowDate());
        return serverRegionMapper.insertServerRegion(serverRegion);
    }

    /**
     * 修改区服
     *
     * @param updateDTO 区服
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateServerRegion(ServerRegionUpdateDTO updateDTO) {
        //判断是否存在
        Long count = serverRegionMapper.selectCount(new QueryWrapper<ServerRegion>().lambda()
                .eq(ServerRegion::getRegionName, updateDTO.getRegionName())
                .ne(ServerRegion::getRegionId, updateDTO.getRegionId())
        );
        if (count > 0) {
            throw new ServiceException("该区服《" + updateDTO.getRegionName() + "》已存在，请勿重复添加");
        }

        ServerRegion serverRegion = BeanUtil.copyProperties(updateDTO, ServerRegion.class);
        serverRegion.setUpdateBy(updateDTO.getLoginUser().getUsername());
        serverRegion.setUpdateTime(DateUtils.getNowDate());
        return serverRegionMapper.updateServerRegion(serverRegion);
    }

    /**
     * 批量删除区服
     *
     * @param regionIds 需要删除的区服主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteServerRegionByRegionIds(Long[] regionIds) {
        return serverRegionMapper.batchDeleteServerRegionByRegionIds(regionIds);
    }

    /**
     * 删除区服信息
     *
     * @param regionId 区服主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteServerRegionByRegionId(Long regionId) {
        return serverRegionMapper.deleteServerRegionByRegionId(regionId);
    }

    @Override
    public List<ServerRegion> selectList() {
        return serverRegionMapper.selectList();
    }
}
